package com.hiber.dao;

import java.util.List;

import com.hiber.domain.User;
import com.hiber.util.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.Transaction;



public class UserDAO {
	
	public void addUser(User user) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(user);
		transaction.commit();
	}
	
	public User findUserById(long user_id) {
		User user;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		user = (User) session.get(User.class, user_id);
		transaction.commit();
		return user;
	}
	
	public List findAllUsers() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		List users = session.createQuery("from User").list();
		transaction.commit();
		return users;
	}
	
	public void updateUser(User user) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		session.update(user);
		transaction.commit();
	}
	
	public void deleteUser(long user_id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction transaction = session.beginTransaction();
		session.delete((User) session.get(User.class, user_id));
		transaction.commit();
	}
	
}
